<?php !defined( 'SUNCMS' ) && exit( 'No direct script access allowed'  );

/**
 * 
 * Sun CMS
 * 
 * @author			Burning
 * @copyright		Copyright (c) 2009, IsunCms.Com.
 * @license			http://www.isuncms.com/license.htm
 * @link				http://www.isuncms.com/
 * @since				Version 1.0 Beta
 * 
 */

function display( $template, $path = '' )
{
	if( defined( 'SUN_ADMIN' ) )
	{
		//$path = 'admin';
		//$tpl_file = SUNCMS.'admin/template/'.$template.'.htm';
		return SUNCMS.'template/system/'.$template.'.php';
	}
	else
	{
		$path == '' && $path = DEFAULT_TEMP;
		$tpl_file = SUNCMS.'template/'.$path.'/'.$template.'.htm';
		if( !file_exists( $tpl_file ) )
		{
			$path = 'default';
			$tpl_file = SUNCMS.'template/default/'.$template.'.htm';
		}
	}
	if( !file_exists( $tpl_file ) )
	{
		show_error( '模板文件：'.$template.'.htm 不存在！' );
		exit;
	}
	$cache_file = SUNCMS.'datas/template_compile/'.$path.'/'.$template.'.php';
	if( TEMPLATE_RECOMPLIE || !file_exists( $cache_file ) || ( @filemtime( $cache_file ) <= @filemtime( $tpl_file ) ) )
	{
		template_compile( $tpl_file, $cache_file );
	}
	return $cache_file;
}

function template_compile( $tpl_file, $cache_file )
{
	$content = @file_get_contents( $tpl_file );
	$path = dirname( $cache_file );
	if( !is_dir( $path ) )
	{
		if( !create_dir( $path ) )
		{
			show_error( '无法建立缓存目录:'.$path );
			exit;
		}
	}
	$content = template_parse( $content );
	$strlen = @file_put_contents( $cache_file, $content );
	@chmod( $cache_file, 0777 );
	return $strlen;
}

function template_parse( $template )
{
	$var_regexp = "((\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\[[a-zA-Z0-9_\-\.\"\'\[\]\$\x7f-\xff]+\])*)";
	$template = preg_replace("/([\n\r]+)\t+/s", "\\1", $template);
	$template = preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}", $template);
	//替换直接变量输出
    $template = preg_replace("/(\\\$[a-zA-Z0-9_\[\]\'\"\$\x7f-\xff]+)\.([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/s", "\\1['\\2']", $template);
	$template = preg_replace("/\{(\\\$[a-zA-Z0-9_\[\]\'\"\$\.\x7f-\xff]+)\}/s", "<?=\\1?>", $template);
	$template = preg_replace("/\{(\\\$[a-zA-Z0-9_\[\]\'\"\$\.\x7f-\xff]+)\}/s", "<?=\\1?>", $template);

	$template = preg_replace("/$var_regexp/es", "addquote('<?=\\1?>')", $template);
	$template = preg_replace("/\<\?\=\<\?\=$var_regexp\?\>\?\>/es", "addquote('<?=\\1;?>')", $template);
	
	//template
	$template = preg_replace("/[\n\r\t]*\{template\s+([a-z0-9_]+)\}[\n\r\t]*/is", "\n<?php include display('\\1'); ?>\n", $template);
	$template = preg_replace("/[\n\r\t]*\{template\s+(.+?)\}[\n\r\t]*/is", "\n<?php include display(\\1); ?>\n", $template);
	//include
	$template = preg_replace("/[\n\r\t]*\{include\s+(.+)\}[\n\r\t]*/is", "\n<?php include \\1; ?>\n", $template);
    //block
    $template = preg_replace("/[\n\r\t]*\{block\s+([a-z0-9_]+)\}[\n\r\t]*/is","\r\n<?php echo block('\\1'); ?>\r\n",$template);
    $template = preg_replace("/[\n\r\t]*\{block\s+(.+?)\}[\n\r\t]*/is","\r\n<?php echo block('\\1'); ?>\r\n",$template );
	//tag
	$template = preg_replace("/[\n\r\t]*\{tag\s+([^}]+)\}[\n\r\t]*/e", "get_tag('\\1')", $template);
	$template = preg_replace("/[\n\r\t]*\{\/tag\}[\n\r\t]*/is","<?php } unset(\$DATA); ?>",$template);
	//php
	$template = preg_replace("/[\n\r\t]*\{php\s+(.+?)\}[\n\r\t]*/ies", "stripvtags('\n<?php \\1; ?>\n','')", $template);
	//echo
	$template = preg_replace("/[\n\r\t]*\{echo\s+(.+?)\}[\n\r\t]*/ies", "stripvtags('\n<?php echo \\1; ?>\n','')", $template);
	//elseif
	$template = preg_replace("/[\n\r\t]*\{elseif\s+(.+?)\}[\n\r\t]*/ies", "stripvtags('\n<?php } elseif(\\1) { ?>\n','')", $template);
	//else
	$template = preg_replace("/[\n\r\t]*\{else\}[\n\r\t]*/is", "\n<?php } else { ?>\n", $template);
	for ($i = 0; $i < 5; $i++) {
        $template = preg_replace("/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\}[\n\r]*(.+?)[\n\r]*\{\/loop\}[\n\r\t]*/ies","stripvtags('<?php if(is_array(\\1)) { foreach(\\1 as \\2) { ?>\n','\\3<?php } } ?>')", $template);
        $template = preg_replace("/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}[\n\r\t]*(.+?)[\n\r\t]*\{\/loop\}[\n\r\t]*/ies","stripvtags('<?php if(is_array(\\1)) { foreach(\\1 as \\2 => \\3) { ?>\n','\\4<?php } } ?>')",$template);
        $template = preg_replace("/[\n\r\t]*\{for\s+(.+?)\}[\n\r]*(.+?)[\n\r]*\{\/for\}[\n\r\t]*/ies","stripvtags('<?php for(\\1) { ?>\n','\\2<?php } ?>')", $template);
        $template = preg_replace("/([\n\r\t]*)\{if\s+(.+?)\}([\n\r]*)(.+?)([\n\r]*)\{\/if\}([\n\r\t]*)/ies","stripvtags('\\1<?php if(\\2) { ?>\\3','\\4\\5<?php } ?>\\6')",$template);
    }
	//常量替换
    $template = preg_replace("/\{([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)\}/s","<?php echo \\1?>",$template);
	//删除 PHP 代码断间多余的空格及换行
    $template = preg_replace("/ \?\>[\n\r]*\<\? /s", " ", $template);
    $template = preg_replace("/ \?\>[\n\r\s]*\<\?php /s", " ", $template);
    $template = str_replace("?><?php", " ", $template);
    $template = str_replace("?><?=", "echo ", $template);
    $template = "<?php !defined('SUNCMS') && exit('No direct script access allowed'); ?>".$template;
	return $template;    
}

/**
 * 正则表达式匹配替换
 *
 * @param string $var ：
 * @return 
 */
function addquote($var) 
{
	return str_replace("\\\"", "\"", preg_replace("/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s", "['\\1']", $var));
}

/**
 * 正则表达式匹配替换
 *
 * @param string $expr ：
 * @param string $statement ：
 * @return 
 */
function stripvtags( $expr, $statement='' ) 
{
	$expr = str_replace("\\\"", "\"", preg_replace("/\<\?\=(\\\$.+?)\?\>/s", "\\1", $expr));
	$statement = str_replace("\\\"", "\"", $statement);
	return $expr.$statement;
}

function block_parse()
{
	
}

function tag_parse()
{
	
}

function label_parse()
{
	
}